package com.example.demo.service;


import com.example.demo.entity.Authority;
import com.example.demo.entity.Customer;
import com.example.demo.mapper.CustomerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("customerService")
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public int registerUser(Customer customer) {
        return customerMapper.registerUser(customer);
    }

    @Override
    public Customer getCustomer(String username) {
        Customer o = (Customer) redisTemplate.opsForValue().get("customer_" + username);
        if (o == null) {
            Customer customer = customerMapper.getCustomer(username);
            redisTemplate.opsForValue().set("customer_" + username, customer);
        }
        return (Customer) redisTemplate.opsForValue().get("customer_" + username);
    }

    @Override
    public List<Authority> getAuthoris(String username) {
        Object o = redisTemplate.opsForValue().get("authority_" + username);
        if (o != null) {
            return (List<Authority>) o;
        }
        List<Authority> authorities = customerMapper.getAuthoris(username);
        if (authorities.size() > 0) {
            redisTemplate.opsForValue().set("authority_" + username, authorities);
        }

        return authorities;
    }


}
